//
//  YBWIAPViewController.m
//  YBWIAPModule
//
//  Created by ZXJ on 12/12/2018.
//  Copyright (c) 2018 ZXJ. All rights reserved.
//

#import "YBWIAPViewController.h"
#import "YBWBaseMeditor+YBWAccountModule.h"
#import "YBWBaseMeditor+YBWIAPModule.h"
#import "MBProgressHUD.h"
#import "YBWToast.h"


@interface YBWIAPViewController ()

@end

@implementation YBWIAPViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([[YBWBaseMeditor sharedInstance] isLogin]) {
        
        [self setTitle:[NSString stringWithFormat:@"UserId：%@",[YBWBaseMeditor sharedInstance].userId]];

    }
    
    UIButton * payButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [payButton setTitle:@"IAP支付" forState:UIControlStateNormal];
    [payButton.layer setCornerRadius:22.0];
    [payButton.layer setBorderColor:[UIColor grayColor].CGColor];
    [payButton.layer setBorderWidth:0.42];
    [payButton setFrame:CGRectMake(CGRectGetWidth(self.view.frame)/2.0-80.0, CGRectGetHeight(self.view.frame)/2.0-44.0/2.0, 160.0, 44.0)];
    [payButton addTarget:self action:@selector(actionIAPEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:payButton];
    
    
}

- (void)actionIAPEvent:(UIButton *)button{
    
    if (![[YBWBaseMeditor sharedInstance] isLogin]) {
        
        UINavigationController * nav = [[YBWBaseMeditor sharedInstance] YBWMediator_LoginNavigationControllerWithComplete:^(NSString * _Nullable message, BOOL success) {
            if (success) {
                NSLog(@"登录成功");
                [self setTitle:[NSString stringWithFormat:@"UserId：%@",[YBWBaseMeditor sharedInstance].userId]];
            }
        }];
        [self presentViewController:nav animated:YES completion:nil];
        
    }else{
        
        NSString * productID = @"com.kmcc.cs51ztb.coin50";
        NSString * orderCode = @"123";
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        [[YBWBaseMeditor sharedInstance] YBWMediator_startPurchWithProductID:productID orderCode:orderCode completion:^(BOOL success, NSString *message) {
            
            NSLog(@"Message:%@",message);
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [YBWToast showWithErrorMsg:message];
            
        }];
        
    }
    
}

@end
